# GuaNameGenerator.py
from constants import BAGUA
from utils.format_lst import is_format, format_max3_to_max2
from mapping.numLst2idx import GuaConverter
from GuaNameMapper import GuaNameMapper

class GuaNameGenerator:
    def __init__(self, num_lst: list[int]):
        self.num_lst = num_lst

    def to_half_gua_name(self) -> str | None:
        """
        已知 numLst，获取主卦(半)变卦(半)全称
        :return: 主卦(半)变卦(半)全称
        """
        if not is_format(self.num_lst, 'Len3Max3', right_len=3):
            return None
        origin, changed = format_max3_to_max2(self.num_lst)
        origin_gua = GuaConverter(origin).to_name_max2('halfGua')
        if origin == changed:
            return origin_gua
        else:
            changed_gua = GuaConverter(changed).to_name_max2('halfGua')
            return origin_gua + '化' + changed_gua

    def to_name(self) -> str | None:
        """
        已知 numLst，获取主卦变卦全称
        :return: 主卦变卦全称
        """
        if not is_format(self.num_lst, 'Len3Max3', right_len=6):
            return None
        origin, changed = format_max3_to_max2(self.num_lst)
        origin_gua = GuaConverter(origin).to_name_max2()
        if origin == changed:
            return origin_gua
        else:
            changed_gua = GuaConverter(changed).to_name_max2()
            return origin_gua + '之' + changed_gua

    def to_name_max2(self, mode: str = 'gua') -> str:
        """
        已知 numLst 求卦名
        :param mode: 模式，可选值为 'gua' 或 'halfGua'
        :return: 卦名
        """
        if not is_format(self.num_lst, 'Max2'):
            raise ValueError('numLst 所有值应该介于1，2之间，而你的 numLst =', self.num_lst)
        idx = GuaConverter(self.num_lst).get_nature_idx()
        if mode == 'gua' or len(self.num_lst) == 6:
            return GuaNameMapper.by_idx(idx)
        elif mode == 'halfGua' or len(self.num_lst) == 3:
            return BAGUA[idx]
        else:
            raise ValueError('numLst len应为3或6，numLst =', self.num_lst)